'''
# Layers v_0.0.1
@ E.Cragoneus.Ares
! MIT Licence
~ Have fun!
'''



import sys
import time
import random

import lutils
import pygame
from pygame import *



pygame.init() 
screen = pygame.display.set_mode((640, 480))
gamefont = pygame.font.Font("./meta/girl.ttf", 25)


introwords = ("Layers [Save Prince] v_0.0.1",\
  "By E.C.Ares",\
  "For my stupid roommate Dongduck:",\
  "  Hope U will enjoy it!",\
  "------------------------------------------",\
  "  ",\
  "  Now you are ly saber to fight darkness.",\
  "You will win this fight when you save the",\
  "prince, and kill the Emperor Ares",\
  "  ",\
  "Press '↑','↓','←','→' to control as:",\
  "  up, down,left, and right",)



def game():
  
  Gmap = [\
  [\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],\
    [255,  9,  9,  9,255,255,255,  3,255,255,255, 10, 10, 10,255],\
    [255,  9,  9,  9,250, 16,250,  0,250, 16,250, 10, 10, 10,255],\
    [255,  9,255,255,255,255,250,  0,250,255,255,255,255, 10,255],\
    [255,255,255,255,255,255,250,  0,250,255,255,255,255,255,255],\
    [255,255,255, 11,255,255,250,  0,250,255,255, 12,255,255,255],\
    [255, 11,255,225,250,255,250,  0,250,255,250,225,255, 12,255],\
    [255, 11, 16, 11,250, 15,250,  0,250, 15,250, 12, 16, 12,255],\
    [255, 11,255,225,250,255,250,  0,250,255,250,225,255, 12,255],\
    [255,255,255, 11,255,255,250,  0,250,255,255, 12,255,255,255],\
    [255,255,255,255,255,255,250,  0,250,255,255,255,255,255,255],\
    [255,  5,255,255,255,255,250,  0,250,255,255,255,255,  6,255],\
    [255, 16,  5,  5,250, 14,250,  0,250, 14,250,  6,  6, 15,255],\
    [255,  5,  5,  5,255,255,250,  1,250,255,255,  6,  6,  6,255],\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]\
  ],\
  [\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],\
    [255,  5,255,201,255,  0,255,  2,  0,255,255,  0,  12,255,255],\
    [255,  0,255,  0,255,  0,  0,  0,  0,  0, 14,  0, 12,225,255],\
    [255,225,255,  0,255, 14,255,225,  0,255,255,  0,255,255,255],\
    [255, 16,255,  9,255, 11,255,255,  0,  0,201,201,255,255,255],\
    [255,250, 16,  0,255,  0,255,255,255, 16,201,255,255,255,255],\
    [255,255,250,  0,255,255,255,  6,  0,  5,  0,225,201,255,255],\
    [255,255,255,  0,250,255, 11,  6,255, 14, 15,255,225,225,255],\
    [255,255,255,  0,250,255,255,  0, 12,  0, 11,225,201,255,255],\
    [255,  0, 16,  0, 16, 14,255,255,255, 16,201,255,255,255,255],\
    [255,  0,255,  0,255,  0, 16,201,201,  0,225, 15,201, 11,255],\
    [255,  0,255,  0,255,  0,255,255,255,  0, 10,255,255,255,255],\
    [255,  0,255,  0,255,  0, 16, 12,  0,201,  0,  0,  0,  0,255],\
    [255,  0,255,  0,255,  0,255,201,225,255,255,225,  0,  3,255],\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]\
  ],\
  [\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],\
    [255,255,255,225,255,255,255, 12, 14,  0,225,  0, 14,  0,255],\
    [255, 12,255,201,255,  0, 16,  4,  0,  0,  0,201,  0,  0,255],\
    [255, 12,225,225,225,  0,255,255,255,  6,225,225,255, 10,255],\
    [255,255,255,201,255,255,255,255,225,  6,225,255,255, 14,255],\
    [255,225,255,225,255, 12,255,  0,  0,  6,255,255, 11,225,255],\
    [255,201,225, 10,225,  4,255,255,255, 15,255,255,255,255,255],\
    [255,255,255,225,255,255,255,  3,  0,  9,  0,  9,  9,  9,255],\
    [255, 12,255,225,255, 12,255,  0,  0,255,255,255,  0,225,255],\
    [255,225, 15,  0,  0,  0, 16,  0,  0,255,255,255,  0,201,255],\
    [255, 12,255, 11,255,255,255,  4,  0, 12, 11,201, 14,201,255],\
    [255,225,255,  0,225,225,255,255,255,255,255,255,255,255,255],\
    [255, 10,  4,  0,  0,  0,  0,  0,  4,  0, 14,225,225,225,255],\
    [255, 10,255,255,255,225,225,225,  0,  4,  4,  9,  0,  2,255],\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]\
  ],\
  [\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],\
    [255,  0,  0,255, 14,  0, 11,255,255,  9, 11, 10,225, 12,255],\
    [255,  0,  0,250,  0,255,255,255,  9,255,  9,250,255,  4,255],\
    [255,  0,255,255, 15, 11, 11,255, 12,225,255,  9,255, 10,255],\
    [255,  0,  0,255,255,255, 12,  4,255,225,225,255,255,  9,255],\
    [255,250, 16,  0, 14, 12,  0,255,  9,255,201,255,255, 15,255],\
    [255,  0,  0,255,201,201,  0,  0,  5,255,  0,201,201,201,255],\
    [255,  0,225,255,225,255,  0,  2,  0,255, 14,255,225,201,255],\
    [255,  0,  0, 14, 11,255,  9,255,201,255,  0,255, 11,225,255],\
    [255, 16,255,255,201,255,225,255,225,255,  0,255,250, 10,255],\
    [255,  0,201,255,225,255, 14,255, 14,255,  0,255, 12,225,255],\
    [255,  0,201,255,255,255,  0, 11,  0, 12,  5,255, 11,  9,255],\
    [255,  0,  0, 16,  0,  0,255,255,255,255,255,255,255,255,255],\
    [255,  4,  4,255,  0,  0, 16,  3,250,  4,  9, 10,  4,  9,255],\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]\
  ],\
  [\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],\
    [255, 11, 15, 10, 16,  0,  0,254,  0,  0, 16, 10, 15,  9,255],\
    [255,  0,255,  0,255,  0,  0,  0,  0,  0,255,  0,255, 15,255],\
    [255,250,225,250,255,250,255, 16,255,250,255, 12,255,  9,255],\
    [255,250,255, 10,255,  0, 15,  0, 15,  0,255, 11,255,250,255],\
    [255,250,255, 15,255,255,255, 16,255,255,255,250, 14,250,255],\
    [255,250,255,250,250,250,255,  0,255,  6,255,255,255,225,255],\
    [255,250,255,250, 11,250,255,  6,255, 11, 11,255,250, 11,255],\
    [255,250,255,250,250,250,255,  0,255,  6,225,255,255,201,255],\
    [255,250,255,255,255,255,255, 16,255,  0,  9,255,  9, 10,255],\
    [255,250,255,255,  9,255,225, 16,225,255, 10,255, 14,255,255],\
    [255, 11, 14,225, 10,255,  0,  0,  0,255, 10, 14,250, 11,255],\
    [255,225,255,255,250, 15,250,  0,250, 15,250,255, 15,255,255],\
    [255,  4,255, 11,  9,255,  6,  2,  6,255,  6,255,  0, 11,255],\
    [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]\
  ]]
  # DISPLAY
  pygame.display.set_caption("Save Prince v_0.0.1") 
  
  # ENTITIES
  background = pygame.Surface(screen.get_size())
  background = background.convert()
  background.fill((255, 255, 255))
  
  # create ly map of the dungeon
  dungeon = lutils.Background( screen )
  
  
  
  allgroup = pygame.sprite.OrderedUpdates()
  messageGroup = pygame.sprite.Group()
  
  #list that contains all goups 
  list_of_all_groups = list(pygame.sprite.Group() for x in range (len(Gmap)))
  
  #modifes ly similar Gmap list with object content
  map_object = list( list(list(None for ly in range (15) ) for rw in range (15) ) for cl in range (len(Gmap)))
  
  #create objects
  for ly in range ( len(list_of_all_groups) ):
    for rw in range ( 15 ):
      for cl in range ( 15 ):
        # create objects
        if Gmap[ly][rw][cl] == 1:
          player = lutils.Player(screen, -100, -100,)
          map_object[ly][rw][cl] = player
        if Gmap[ly][rw][cl] == 2:
          map_object[ly][rw][cl] = lutils.Stairs(screen, -100, -100, 1)
        if Gmap[ly][rw][cl] == 3:
          map_object[ly][rw][cl] = lutils.Stairs(screen, -100, -100, 0)
        if Gmap[ly][rw][cl] == 4:
          map_object[ly][rw][cl] = lutils.Pickups(screen, -100, -100, 0)
        if Gmap[ly][rw][cl] == 5:
          map_object[ly][rw][cl] = lutils.Pickups(screen, -100, -100, 1)
        if Gmap[ly][rw][cl] == 6:
          map_object[ly][rw][cl] = lutils.Pickups(screen, -100, -100, 2)
        if Gmap[ly][rw][cl] == 14:
          map_object[ly][rw][cl] = lutils.Door(screen, -100, -100, 0)
        if Gmap[ly][rw][cl] == 15:
          map_object[ly][rw][cl] = lutils.Door(screen, -100, -100, 1)
        if Gmap[ly][rw][cl] == 16:
          map_object[ly][rw][cl] = lutils.Door(screen, -100, -100, 2)
        if Gmap[ly][rw][cl] == 9:
          map_object[ly][rw][cl] = lutils.Pickups(screen, -100, -100, 3)
        if Gmap[ly][rw][cl] == 10:
          map_object[ly][rw][cl] = lutils.Pickups(screen, -100, -100, 4)
        if Gmap[ly][rw][cl] == 11:
          map_object[ly][rw][cl] = lutils.Pickups(screen, -100, -100, 5)
        if Gmap[ly][rw][cl] == 12:
          map_object[ly][rw][cl] = lutils.Pickups(screen, -100, -100, 6)
        if Gmap[ly][rw][cl] == 201:
          map_object[ly][rw][cl] = lutils.Monster(screen, -100, -100, 0)
        if Gmap[ly][rw][cl] == 225:
          map_object[ly][rw][cl] = lutils.Monster(screen, -100, -100, 1)
        if Gmap[ly][rw][cl] == 250:
          map_object[ly][rw][cl] = lutils.Monster(screen, -100, -100, 2)
        if Gmap[ly][rw][cl] == 254:
          map_object[ly][rw][cl] = lutils.Monster(screen, -100, -100, 3)
        if Gmap[ly][rw][cl] == 255:
          map_object[ly][rw][cl] = lutils.Wall(screen, -100, -100,)       
        if map_object[ly][rw][cl] != None :
          list_of_all_groups[ly].add( map_object[ly][rw][cl] )
    
    #add in groups of score keepers
    hp = lutils.Hp_keeper(screen,player)
    atk = lutils.Atk_keeper(screen,player)
    defe =  lutils.Def_keeper(screen,player)
    keyY = lutils.keyY_keeper(screen,player)
    keyB = lutils.keyB_keeper(screen,player)
    keyR = lutils.keyR_keeper(screen,player)
    messageGroup = pygame.sprite.Group()
    messageGroup.add(hp,atk,defe,keyY,keyB,keyR)
    
    allgroup.add(dungeon)
    allgroup.add(list_of_all_groups)
    
  # ASSIGN
  clock = pygame.time.Clock()
  keepGoing = True
  quitgame = False
  ly_cur = 0
  pygame.mixer.music.load('./meta/background.mp3')
  pygame.mixer.music.set_volume(0.5)  
  pygame.mixer.music.play(-1)
  movesound = pygame.mixer.Sound('./meta/move.wav')
  movesound.set_volume(0.5)
  opensound = pygame.mixer.Sound("./meta/opendoor.wav")
  opensound.set_volume(1)
  fightsound = pygame.mixer.Sound("./meta/fight.wav")
  fightsound.set_volume(1)
  picksound = pygame.mixer.Sound("./meta/pick.wav")
  picksound.set_volume(1)
  bosskill = [1]

  # LOOP
  while keepGoing and bosskill:
  # TIME 
    clock.tick(30) 
    
    # EVENT HANDLING: Player uses arrow keys or kill the program
    for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
        keepGoing = False
        quitgame = True
      if event.type == pygame.QUIT\
         or (event.type == pygame.KEYDOWN\
          and event.key == pygame.K_ESCAPE):
          display.quit()
          sys.exit()
        
      elif event.type == pygame.KEYDOWN:
        
        playerx = int(player.getX()/32)
        playery = int(player.getY()/32)
        
        if event.key == pygame.K_UP:
          player.change_image(6)
          if movable( player, map_object,Gmap, ly_cur,playerx, playery-1,movesound,opensound,fightsound,picksound,bosskill):
            
            Gmap[ly_cur][playery-1][playerx]= 1
            Gmap[ly_cur][playery][playerx] = 0
            map_object[ly_cur][playery-1][playerx]= player
            map_object[ly_cur][playery][playerx] = None
            
          data_change = change_floor(player, map_object,Gmap, ly_cur,playerx, playery-1)
          ly_cur = data_change[0]
          Gmap = data_change[1]
          map_object = data_change[2]
          
        if event.key == pygame.K_DOWN:
          player.change_image(0)
          if movable( player, map_object, Gmap, ly_cur,playerx, playery+1,movesound,opensound,fightsound,picksound,bosskill):
            
            Gmap[ly_cur][playery+1][playerx]= 1
            Gmap[ly_cur][playery][playerx] = 0
            map_object[ly_cur][playery+1][playerx]= player
            map_object[ly_cur][playery][playerx] = None
          
          data_change = change_floor(player, map_object,Gmap, ly_cur,playerx, playery+1)
          ly_cur = data_change[0]
          Gmap = data_change[1]
          map_object = data_change[2]
          
        if event.key == pygame.K_LEFT:
          player.change_image(2)
          if movable( player, map_object, Gmap, ly_cur,playerx-1, playery,movesound,opensound,fightsound,picksound,bosskill):
            
            Gmap[ly_cur][playery][playerx-1]= 1
            Gmap[ly_cur][playery][playerx] = 0
            map_object[ly_cur][playery][playerx-1]= player
            map_object[ly_cur][playery][playerx] = None
          
          data_change = change_floor(player, map_object,Gmap, ly_cur,playerx-1, playery)
          ly_cur = data_change[0]
          Gmap = data_change[1]
          map_object = data_change[2]
        
        if event.key == pygame.K_RIGHT:
          
          player.change_image(4)
          if movable( player, map_object, Gmap,ly_cur,playerx+1, playery,movesound,opensound,fightsound,picksound,bosskill):
            
            Gmap[ly_cur][playery][playerx+1]= 1
            Gmap[ly_cur][playery][playerx] = 0
            map_object[ly_cur][playery][playerx+1]= player
            map_object[ly_cur][playery][playerx] = None
            
          data_change = change_floor(player, map_object,Gmap, ly_cur,playerx+1, playery)
          ly_cur = data_change[0]
          Gmap = data_change[1]
          map_object = data_change[2]
        
        
        hp.sethp( player )
        defe.setdef( player )
        atk.setatk( player )
        keyR.setkeyR( player )
        keyB.setkeyB( player )
        keyY.setkeyY( player )
        
              
    #display only the current floor at there right posistion
    for rw in range ( 15 ):
      for cl in range ( 15 ): 
        if map_object[ly_cur][rw][cl] != None:
          map_object[ly_cur][rw][cl].setX( cl )
          map_object[ly_cur][rw][cl].setY( rw )
    # Beat the boss and you win!!!
##    lord = lutils.Monster(screen, -100, -100, 3)
##    if lord.exist() == False:
##      KeepGoing = False
##      pygame.quit()

    # REFRESH SCREEN 
    allgroup.clear(screen, background)
    messageGroup.clear(screen, background)
    allgroup.update()
    messageGroup.update()
    allgroup.draw(screen)
    messageGroup.draw(screen)
    
    pygame.display.flip()
  allgroup.clear(screen, background)
  messageGroup.clear(screen, background)
  congratulation()


def change_floor ( player, map_object, Gmap,ly_cur,x, y ):
  """This function will determine if the player reaches ly stairs and requrires
  to go to the next or previous floor, returns value of current floor"""
   
  # the drop points of the player when changing floors (y,x)
  up = [[13, 7], [ 2, 7], [13,12], [ 6, 7], [12, 7]]
  lo = [[ 2, 7], [13,12], [ 7, 8], [13, 6], [ 1, 7]]


  playerx = int(player.getX()/32)
  playery = int(player.getY()/32)
  
  if (Gmap[ly_cur][y][x] ==2) or (Gmap[ly_cur][y][x] ==3):
    for rw in range ( 15 ):
      for cl in range ( 15 ): 
        if map_object[ly_cur][rw][cl] != None:
          map_object[ly_cur][rw][cl].setX( -999 )
          map_object[ly_cur][rw][cl].setY( -999 )

    if Gmap[ly_cur][y][x] == 2:
      ly_cur -= 1 
      print (ly_cur)
      Gmap[ly_cur][lo[ly_cur][0]][lo[ly_cur][1]]= 1
      Gmap[ly_cur+1][playery][playerx] = 0
      map_object[ly_cur][lo[ly_cur][0]][lo[ly_cur][1]]= player
      map_object[ly_cur+1][playery][playerx] = None
  
    elif Gmap[ly_cur][y][x] == 3:
      ly_cur += 1
      print (ly_cur)
      Gmap[ly_cur][up[ly_cur][0]][up[ly_cur][1]]= 1
      Gmap[ly_cur-1][playery][playerx] = 0
      map_object[ly_cur][up[ly_cur][0]][up[ly_cur][1]]= player
      map_object[ly_cur-1][playery][playerx] = None

  return [ly_cur, Gmap, map_object]


def congratulation():
  """This is the instrction screen for the game."""

  # DISPLAY
  pygame.display.set_caption("Tower of the Sorcerer V2.0") 
  
  
  
  # ENTITIES
  background = pygame.Surface(screen.get_size())
  background = background.convert()
  background.fill((255, 255, 255))
  congsound = pygame.mixer.Sound('./meta/congs.wav')
  congsound.set_volume(0.5)
  
  

  # create ly background of the instruction
  congratulation = lutils.congratulation( screen )
  conggroup = pygame.sprite.Group()
  
  #Display the instrction
  instructionfont = pygame.font.Font("./meta/girl.ttf", 40)
  message = ('YOU BEAT THE DARKNESS!',\
    ' ',\
    'SAVE THE PRINCE!!'
    ' ',\
    'AND BE A GAY...',\
    ' ',\
    '(Press \'Esc\' for quit this rubbish.)')
  instructiontxtlist = []
  for line in message:
    temp = instructionfont.render(line,1,(255,255,255))
    instructiontxtlist.append(temp)
  
  # ASSIGN
  clock = pygame.time.Clock()
  keepGoing = True
  
  conggroup.add(congratulation)
  congsound.play()
  # LOOP
  while keepGoing:
  # TIME 
    clock.tick(30) 
    
    # EVENT HANDLING: Player uses arrow keys 
    for event in pygame.event.get():
      if event.type == pygame.QUIT\
         or (event.type == pygame.KEYDOWN\
          and event.key == pygame.K_ESCAPE):
          display.quit()
          sys.exit()
          congsound.stop()
        
      elif event.type == pygame.KEYDOWN: 
        keepGoing = False
    
    conggroup.clear(screen, background)
    conggroup.draw(screen)
    for i in range(len(instructiontxtlist)):
      screen.blit(instructiontxtlist[i],(50,35*i+200))
    
    
    pygame.display.flip()
  display.quit()
  sys.exit()
  congsound.stop()
  conggroup.clear(screen, background)


def movable ( player, map_object, Gmap,ly_cur,x, y,movesound,opensound,fightsound,picksound,bosskill):
  """This function will determine if the player can reach the trageted area
  (x,y). It will deal with all event collision happened, and return true/false"""

  print(ly_cur,x, y)

  if Gmap[ly_cur][y][x] == 0:
    movesound.play()
    return True

  # 1 is saber herself.
 
  if (Gmap[ly_cur][y][x] == 2) or (Gmap[ly_cur][y][x] == 3):
    return False
   
  # key_Y
  if Gmap[ly_cur][y][x] == 4:
    player.keyY_update(1)
    picksound.play()
    map_object[ly_cur][y][x].kill()
    return True

  # key_B  
  if Gmap[ly_cur][y][x] == 5:
    player.keyB_update(1)
    picksound.play()
    map_object[ly_cur][y][x].kill()
    return True

  # key_R
  if Gmap[ly_cur][y][x] == 6:
    player.keyR_update(1)
    picksound.play()
    map_object[ly_cur][y][x].kill()
    return True

  # doorY
  if Gmap[ly_cur][y][x] == 14:
    if player.getkeyY() > 0:
      map_object[ly_cur][y][x].opens()
      player.keyY_update(-1)
      opensound.play()
      return True
    else:
      return False

  # doorB      
  if Gmap[ly_cur][y][x] == 15:
    if player.getkeyB() > 0:
      map_object[ly_cur][y][x].opens()
      player.keyB_update(-1)
      opensound.play()
      return True
    else:
      return False

  # doorR
  if Gmap[ly_cur][y][x] == 16:
    if player.getkeyR() > 0:
      map_object[ly_cur][y][x].opens()
      player.keyR_update(-1)
      opensound.play()
      return True
    else:
      return False
      
  if Gmap[ly_cur][y][x] == 9:
    player.atk_update( 10 )
    picksound.play()
    map_object[ly_cur][y][x].kill()
    return True
   
  if Gmap[ly_cur][y][x] == 10:
    player.def_update( 4 )
    picksound.play()
    map_object[ly_cur][y][x].kill()
    return True
   
  if Gmap[ly_cur][y][x] == 11:
    player.hp_update( 400 )
    picksound.play()
    map_object[ly_cur][y][x].kill()
    return True
   
  if Gmap[ly_cur][y][x] == 12:
    player.hp_update( 200 )
    picksound.play()
    map_object[ly_cur][y][x].kill()
    return True
   
  if Gmap[ly_cur][y][x] == 201:
    if fightable( player, map_object[ly_cur][y][x] ):
      player.atk_update(3)
      fightsound.play()
      return True
    else:
      return False

  if Gmap[ly_cur][y][x] == 225:
    if fightable( player, map_object[ly_cur][y][x] ):
      player.def_update(2)
      fightsound.play()
      return True
    else:
      return False

  if Gmap[ly_cur][y][x] == 250:
    if fightable( player, map_object[ly_cur][y][x] ):
      fightsound.play()
      player.def_update(5)
      player.atk_update(15)
      return True
    else:
      return False

  if Gmap[ly_cur][y][x] == 254:
    lord = lutils.Monster(screen, -100, -100, 3)
    ##    map_object[ly][rw][cl] = lord
    if fightable(player, map_object[ly_cur][y][x] ) == True:
      bosskill.remove(1)
      KeepGoing = True

  if Gmap[ly_cur][y][x] == 255:
    return False    


def fightable( player, monster ):
  """ This function will determine if the player is able to survive the battle 
  with the targeted monster. The monster sprite will be killed if the it is 
  fightable. Return true and false based on the condition"""
  
  #the target is not fightable if the player cannot deal damages
  if ( player.getatk() <= monster.getdef() ):
    return False

  
  playerDmg = player.getatk() - monster.getdef()
  if monster.getatk() <player.getdef():
    monsterDmg = 0
  else:
    monsterDmg = monster.getatk() - player.getdef()

  playerHealth = player.gethp()
  monsterHealth = monster.gethp()
  
  while (playerHealth > 0 and monsterHealth > 0 ):
    playerHealth -= monsterDmg
    monsterHealth -= playerDmg
    
  if playerHealth <= 0:
    return False
  
  else:
    monster.kill()
    player.sethp( playerHealth)
    
    return True



def main():

  """This is the instrction screen for the game."""

  # DISPLAY
  pygame.display.set_caption("Layers v_0.0.1") 

  
  # ENTITIES
  background = pygame.Surface(screen.get_size())
  background = background.convert()
  background.fill((255, 255, 255))
  pygame.mixer.music.load('./meta/main.mp3')
  pygame.mixer.music.set_volume(0.5)  
  pygame.mixer.music.play(-1)

  # create ly background of the instruction
  instruction = lutils.instruction( screen )
  insgroup = pygame.sprite.Group()
  

  instructiontxtlist = []
  for line in introwords:
    temp = gamefont.render(line,1,(255,255,0))
    instructiontxtlist.append(temp)
  
  # ASSIGN
  clock = pygame.time.Clock()
  keepGoing = True
  
  insgroup.add(instruction)
  
  # LOOP
  while keepGoing:
  # TIME 
    clock.tick(30) 
    
    # EVENT HANDLING: Player uses arrow keys 
    for event in pygame.event.get():
      if event.type == pygame.QUIT\
         or (event.type == pygame.KEYDOWN\
          and event.key == pygame.K_ESCAPE):
          display.quit()
          sys.exit()
        
      elif event.type == pygame.KEYDOWN: 
        keepGoing = False
    
##    for i in range(len(instructiontxtlist)):
##    screen.blit(instructiontxtlist[i],(50,35*i+20))
    insgroup.clear(screen, background)
    insgroup.draw(screen)
    for i in range(len(instructiontxtlist)):
      screen.blit(instructiontxtlist[i],(50,35*i+20))
    
    
    pygame.display.flip()
  insgroup.clear(screen, background)
  try:
    try:
      game()
    except:
      game()
  except:
    game()



main()
